﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;

namespace AuditUpload.util
{
    class fileHandle
    {
        public static bool Readcsv(ref DataTable Table, string FileName)
        {
            Table = new DataTable();
            int i = 0;
            try
            {
                using (StreamReader sr = new StreamReader(FileName, System.Text.Encoding.UTF8))
                {
                    while (sr.Peek() >= 0)
                    {
                        string[] aa = sr.ReadLine().Split(new string[] { "   " }, StringSplitOptions.None);
                        if (i == 0)
                        {
                            for (int j = 0; j < aa.Length; j++)
                            {
                                Table.Columns.Add(aa[j]);
                            }
                        }
                        //else
                        {
                            DataRow newrow = Table.NewRow();
                            for (int j = 0; j < aa.Length; j++)
                            {
                                newrow[j] = (aa[j]);
                            }
                            Table.Rows.Add(newrow);
                        }
                        i++;
                    }
                    sr.Close();
                    sr.Dispose();
                }
                return (true);
            }
            catch
            { return (false); }
        }
        public static bool WriteText(string DOC, string FileName, bool Append)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(FileName, Append, System.Text.Encoding.UTF8))
                {
                    sw.Write(DOC);
                }
                return (true);
            }
            catch
            { return (false); }
        }
        public static bool ReadText(ref string DOC, string FileName)
        {
            try
            {
                using (StreamReader sr = new StreamReader(FileName, System.Text.Encoding.UTF8))
                {
                    DOC = sr.ReadToEnd();
                }
                return (true);
            }
            catch
            { return (false); }
        }
        public static string WriteFileStream(string FileName, byte[] stream)
        {
            string Error = "";
            try
            {
                if (System.IO.File.Exists(FileName))
                { System.IO.File.Delete(FileName); }
                System.IO.FileStream dataWriter = new System.IO.FileStream(FileName, System.IO.FileMode.Create);
                dataWriter.Seek(0, System.IO.SeekOrigin.End);
                dataWriter.Write(stream, 0, stream.Length);
                dataWriter.Close();
            }
            catch (System.Exception ex)
            {
                Error = "Error!" + ex.ToString();
            }
            return (Error);
        }

    }
}
